package Datalog

case class DatalogPredicate(name: String, arity: Int)

class DatalogAtom(predicate: DatalogPredicate, datalogTerms: List[DatalogTerm]) {
  require(datalogTerms.length == predicate.arity)

  val terms = datalogTerms
  val pred = predicate

  override def toString() = "{" + pred.name + ", " + terms + "}"

  def isSimilarTo(path: DatalogPath): Boolean =
    this.terms(0) == path.start && this.terms(1) == path.destination
}

object DatalogAtom {
  def createAtom(name: String, term1: String, term2: String): DatalogAtom = {
    val t1: DatalogTerm = if (term1.head <= 'Z') ConstantSymbol(term1) else Variable(term1)
    val t2: DatalogTerm = if (term2.head <= 'Z') ConstantSymbol(term2) else Variable(term2)

    new DatalogAtom(DatalogPredicate(name, 2), List(t1, t2))
  }

  def createAtom(name: String, term1: String): DatalogAtom = {
    val t1: DatalogTerm = if (term1.head <= 'Z') ConstantSymbol(term1)
    else Variable(term1)

    new DatalogAtom(DatalogPredicate(name, 1), List(t1))
  }

  def createAtom(name: String): DatalogAtom = {
    new DatalogAtom(DatalogPredicate(name, 0), List())
  }

  def createAtom(name: String, term: DatalogTerm): DatalogAtom = {
    new DatalogAtom(DatalogPredicate(name, 1), List(term))
  }

  def createAtom(name: String, term1: DatalogTerm, term2: DatalogTerm): DatalogAtom = {
    new DatalogAtom(DatalogPredicate(name, 2), List(term1, term2))
  }

}